<?php
class news_and_promo extends WP_Widget {

    function __construct() {
        parent::__construct(
          'news-and-promo',
          'News and Promo',
          array( 'description' => 'Hiển thị bài viết mới bao gồm thumbnail', )
        );
    }

    public function widget($args, $instance) {
            extract($args);

      $title = apply_filters( 'widget_title', $instance['title'] );
      $number = $instance['number'];

            echo $before_widget . $before_title;
            if ( ! empty ( $title ) ) {
              echo $title;
            }
            echo $after_title;

        $args = array (
          'posts_per_page' => $number,
        );
        $neatly_posts = new WP_Query($args);
        echo '<div class="news-promo">';
        echo '<h5>News & Promos</h5>';
        if( $neatly_posts->have_posts() ) {
          while( $neatly_posts->have_posts() ) : $neatly_posts->the_post(); ?>          
            <div class="news">
              <div class="row">
                <div class="col-md-8">
                  <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br>
                  <span style="font-size: 12px; color: #000;"><?php the_time('d/m/Y'); ?></span>
                </div>
                <div class="col-md-4">
                  <?php if ( has_post_thumbnail() ) : the_post_thumbnail('thumbnail'); endif; ?>
                </div>
              </div>
            </div>                
          <?php endwhile;
        }
        echo '</div>';
        wp_reset_postdata();

            echo $after_widget;
        }

    public function form( $instance ) {
        $title = strip_tags($instance['title']);
        if (isset( $instance[ 'number' ] ) ) {
          $number = $instance['number'];
        } else { $number = '5'; }

    ?>
    <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
    <p><em>Use the following options to customize the display.</em></p>

    <p style="border-bottom:4px double #eee;padding: 0 0 10px;">
      <label for="<?php echo $this->get_field_id( 'number' ); ?>">Số lượng bài viết cần hiển thị</label>
      <input id="<?php echo $this->get_field_id( 'number'); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo esc_attr($number); ?>" type="number" style="width:100%;" /><br>
    </p>

    <?php }

    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        $instance['title'] = strip_tags($new_instance['title']);
        $instance['number'] = strip_tags($new_instance['number']);

        return $instance;
    }

}

function register_news_and_promo() {
    register_widget( 'news_and_promo' );
}
add_action( 'widgets_init', 'register_news_and_promo' );
